package mapper;

import Message.entity.Employee;
import anno.AutoFill;
import enumeration.OperationType;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Options;

import java.util.List;

@Mapper
public interface EmployeeMapper {


    /**
     *
     * 根据用户名查询用户
     *
     */
     @Select("select * from employee where username = #{username}")
     Employee login(String username);


    @AutoFill(OperationType.INSERT)
    @Insert("insert into employee (name, username, password, phone, sex, id_number, status, create_time, update_time, create_user, update_user) " +
            "values (#{name}, #{username}, #{password}, #{phone}, #{sex}, #{id_number}, #{status}, #{create_time}, #{update_time}, #{create_user}, #{update_user})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Employee employee);

    @Select("select * from employee where id=#{id}")
    Employee getById(Long id);
    @AutoFill(OperationType.UPDATE)
    @Update("update employee set password=#{password}, update_time=#{update_time} where id =#{id}")
    void setPassword(@Param("id") Long id, @Param("password") String password);

    @Select("select * from employee where id = #{id}")
    Employee getUpdateId(Long id);

    @Select("select count(*) from employee where name like concat('%',#{name},'%')")
    Long getCount(@Param("name") String name);
    
    @Results({
        @Result(column = "id", property = "id"),
        @Result(column = "name", property = "name"),
        @Result(column = "username", property = "username"),
        @Result(column = "password", property = "password"),
        @Result(column = "phone", property = "phone"),
        @Result(column = "sex", property = "sex"),
        @Result(column = "id_number", property = "id_number"),
        @Result(column = "status", property = "status"),
        @Result(column = "create_time", property = "create_time"),
        @Result(column = "update_time", property = "update_time"),
        @Result(column = "create_user", property = "create_user"),
        @Result(column = "update_user", property = "update_user")
    })
    @Select("select * from employee where name like concat('%',#{name},'%') limit #{page},#{pageSize}")
    List<Employee> getPage(@Param("name") String name, @Param("page") Integer page, @Param("pageSize") Integer pageSize);
    @AutoFill(OperationType.UPDATE)
    @Update("update employee set name=#{name},username=#{username},phone=#{phone},sex=#{sex},id_number=#{id_number}, update_time=#{update_time} where id= #{id}")
    int updateEmp(Employee employee);

    @AutoFill(OperationType.UPDATE)
    void updateEmpStatus(Integer status, long id);
}
